{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "plot_expose-mmd_bone.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "#@markdown ■■■■■■■■■■■■■■■■■■\n",
        "\n",
        "#@markdown 【セル①】　ファイルアップロード\n",
        "\n",
        "#@markdown セルを実行して、expose-mmdで出力されたjsonファイルを選択してアップロードしてください\n",
        "\n",
        "#@markdown ■■■■■■■■■■■■■■■■■■\n",
        "\n",
        "from google.colab import files\n",
        "uploaded = files.upload()\n",
        "\n",
        "print(\"■■■■■■■■■■■■■■■\")\n",
        "print(\"■　** OK **\")\n",
        "print(\"■　ファイルアップロードが完了しました\")\n",
        "print(\"■■■■■■■■■■■■■■■\")\n"
      ],
      "metadata": {
        "cellView": "form",
        "id": "Q_fLWHLmAFY5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@markdown ■■■■■■■■■■■■■■■■■■\n",
        "\n",
        "#@markdown 【セル②】　出力対象関節指定\n",
        "\n",
        "#@markdown セルを実行して、描画したい関節名と3Dカラースケールを選択してください\n",
        "\n",
        "#@markdown ■■■■■■■■■■■■■■■■■■\n",
        "\n",
        "import ipywidgets as widgets\n",
        "bone_names = [\"root\", \"center\", \"groove\", \"pelvis\", \"pelvis2\", \"left_hip\", \"right_hip\", \"left_knee\", \"right_knee\", \"left_ankle\", \"right_ankle\", \"left_foot\", \"right_foot\", \"spine1\", \"spine2\", \"neck\", \"head\", \"left_collar\", \"right_collar\", \"head_tail\", \"left_shoulder\", \"right_shoulder\", \"left_elbow\", \"right_elbow\", \"left_wrist\", \"right_wrist\", \"left_wrist_tail\", \"right_wrist_tail\", \"right_eye\", \"left_eye\", \"left_big_toe\", \"left_small_toe\", \"left_heel\", \"right_big_toe\", \"right_small_toe\", \"right_heel\"]\n",
        "w = widgets.Dropdown(\n",
        "    options=bone_names,\n",
        "    value=\"root\",\n",
        "    description='描画関節名: ',\n",
        ")\n",
        "display(w)\n",
        "\n",
        "colorscale_names =  [\"aggrnyl\", \"agsunset\", \"algae\", \"amp\", \"armyrose\", \"balance\", \"blackbody\", \"bluered\", \"blues\", \"blugrn\", \"bluyl\", \"brbg\", \"brwnyl\", \"bugn\", \"bupu\", \"burg\", \"burgyl\", \"cividis\", \"curl\", \"darkmint\", \"deep\", \"delta\", \"dense\", \"earth\", \"edge\", \"electric\", \"emrld\", \"fall\", \"geyser\", \"gnbu\", \"gray\", \"greens\", \"greys\", \"haline\", \"hot\", \"hsv\", \"ice\", \"icefire\", \"inferno\", \"jet\", \"magenta\", \"magma\", \"matter\", \"mint\", \"mrybm\", \"mygbm\", \"oranges\", \"orrd\", \"oryel\", \"oxy\", \"peach\", \"phase\", \"picnic\", \"pinkyl\", \"piyg\", \"plasma\", \"plotly3\", \"portland\", \"prgn\", \"pubu\", \"pubugn\", \"puor\", \"purd\", \"purp\", \"purples\", \"purpor\", \"rainbow\", \"rdbu\", \"rdgy\", \"rdpu\", \"rdylbu\", \"rdylgn\", \"redor\", \"reds\", \"solar\", \"spectral\", \"speed\", \"sunset\", \"sunsetdark\", \"teal\", \"tealgrn\", \"tealrose\", \"tempo\", \"temps\", \"thermal\", \"tropic\", \"turbid\", \"turbo\", \"twilight\", \"viridis\", \"ylgn\", \"ylgnbu\", \"ylorbr\", \"ylorrd\"] \n",
        "cw = widgets.Dropdown(\n",
        "    options=colorscale_names,\n",
        "    value=\"aggrnyl\",\n",
        "    description='3Dカラースケール名: ',\n",
        ")\n",
        "display(cw)\n",
        "\n"
      ],
      "metadata": {
        "cellView": "form",
        "id": "5VAvH2q1BQBm"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@markdown ■■■■■■■■■■■■■■■■■■\n",
        "\n",
        "#@markdown 【セル③】　グラフ描画\n",
        "\n",
        "#@markdown セルを実行してください\n",
        "\n",
        "#@markdown 3D図は、マウスの左ボタンを押しながら回転、右ボタンを押しながら移動、真ん中ボタンを押しながら拡大縮小ができます\n",
        "\n",
        "#@markdown ■■■■■■■■■■■■■■■■■■\n",
        "\n",
        "selected_bname = bone_names[w.index]\n",
        "\n",
        "selected_colorscale = colorscale_names[cw.index]\n",
        "\n",
        "import json\n",
        "json_data = json.loads(uploaded[list(uploaded.keys())[0]].decode(\"utf-8\"))\n",
        "\n",
        "fnos = [int(fno) for fno in json_data.keys()]\n",
        "xs = []\n",
        "ys = []\n",
        "zs = []\n",
        "\n",
        "for fno in fnos:\n",
        "    xs.append(json_data[str(fno)][selected_bname]['x'])\n",
        "    ys.append(json_data[str(fno)][selected_bname]['y'])\n",
        "    zs.append(json_data[str(fno)][selected_bname]['z'])\n",
        "\n",
        "import matplotlib.pyplot as plt\n",
        "from mpl_toolkits.mplot3d import Axes3D\n",
        "import numpy as np\n",
        "\n",
        "plt.figure(figsize=(16, 9))\n",
        "plt.plot(fnos, xs, color = 'red', marker = 'o',  markersize=0.1)\n",
        "plt.title(f'{selected_bname} - X transition graph')\n",
        "plt.xlabel('frame no')\n",
        "plt.ylabel('X transition')\n",
        "plt.show()\n",
        "\n",
        "plt.figure(figsize=(16, 9))\n",
        "plt.plot(fnos, ys, color = 'green', marker = 'o',  markersize=0.1)\n",
        "plt.title(f'{selected_bname} - Y transition graph')\n",
        "plt.xlabel('frame no')\n",
        "plt.ylabel('Y transition')\n",
        "plt.show()\n",
        "\n",
        "plt.figure(figsize=(16, 9))\n",
        "plt.plot(fnos, zs, color = 'blue', marker = 'o',  markersize=0.1)\n",
        "plt.title(f'{selected_bname} - Z transition graph')\n",
        "plt.xlabel('frame no')\n",
        "plt.ylabel('Z transition')\n",
        "plt.show()\n",
        "\n",
        "import plotly.graph_objects as go\n",
        "fig = go.Figure(data=[go.Scatter3d(\n",
        "    x=np.array(xs),\n",
        "    y=np.array(ys),\n",
        "    z=np.array(zs),\n",
        "    hovertemplate='[x: %{x}], [y: %{y}], [z: %{z}]',\n",
        "    mode='markers',\n",
        "    marker=dict(\n",
        "        size=3,\n",
        "        color=np.array(fnos),\n",
        "        colorscale=selected_colorscale,\n",
        "        opacity=0.8\n",
        "    )\n",
        ")])\n",
        "fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))\n",
        "fig.show()\n",
        "\n"
      ],
      "metadata": {
        "cellView": "form",
        "id": "pwbKVn4PCR-n"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}